package com.sskj.qiangjin.ui.activity;

import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.sskj.common.base.App;
import com.sskj.common.util.ClickUtil;
import com.sskj.common.util.DisposUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.lib.RConfig;
import com.sskj.lib.base.BaseActivity;
import com.sskj.qiangjin.R;
import com.sskj.qiangjin.presenter.ForgetPwdPresenter;

import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import io.reactivex.Flowable;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import io.reactivex.subscribers.DisposableSubscriber;

@Route(path = RConfig.APP_FORGET_PWD)//忘记密码
public class ForgetPwdActivity extends BaseActivity<ForgetPwdPresenter> {

    @BindView(R.id.et_forget_tel)
    EditText etForgetTel;
    @BindView(R.id.et_forget_verification_code)
    EditText etForgetVerificationCode;
    @BindView(R.id.tv_forget_get_verification_code)
    TextView tvForgetGetVerificationCode;
    @BindView(R.id.et_forget_pwd)
    EditText etForgetPwd;
    @BindView(R.id.et_forget_pwd_again)
    EditText etForgetPwdAgain;
    @BindView(R.id.tv_confirm)
    TextView tvConfirm;
    @BindView(R.id.tv_forget_login)
    TextView tvForgetLogin;
    private DisposableSubscriber disposableSubscriber;

    @Override
    protected int getLayoutResId() {
        return R.layout.app_activity_forget_pwd;
    }

    @Override
    public ForgetPwdPresenter getPresenter() {
        return new ForgetPwdPresenter();
    }

    @Override
    protected void initView() {
        setTitle(App.INSTANCE.getString(R.string.app_activity_forget_pwd));

        tvConfirm.setEnabled(false);
        ClickUtil.click(tvForgetLogin, this::finish);
        ClickUtil.click(tvForgetGetVerificationCode, () -> {
            if (TextUtils.isEmpty(etForgetTel.getText().toString())) {
                ToastUtil.showShort("请输入手机号");
                return;
            }
            mPresenter.sendCode(etForgetTel.getText().toString().trim());
        });
        ClickUtil.click(tvConfirm, () -> {
            if (TextUtils.isEmpty(etForgetTel.getText().toString())) {
                ToastUtil.showShort("请输入手机号");
                return;
            }
            if (TextUtils.isEmpty(etForgetVerificationCode.getText().toString())) {
                ToastUtil.showShort("请输入验证码");
                return;
            }
            if (TextUtils.isEmpty(etForgetPwd.getText().toString())) {
                ToastUtil.showShort("请输入新密码");
                return;
            }
            if (!etForgetPwd.getText().toString().equals(etForgetPwdAgain.getText().toString())) {
                ToastUtil.showShort("两次密码输入不一致");
                return;
            }
            mPresenter.confirm(etForgetVerificationCode.getText().toString().trim()
                    , etForgetTel.getText().toString().trim()
                    , etForgetPwd.getText().toString().trim());
        });

    }

    public void changeSuccess() {
        finish();
    }

    public void sendCodeOk() {
        startCount();
    }

    public void sendCodeError() {
    }

    public void startCount() {
        tvForgetGetVerificationCode.setEnabled(false);
        DisposUtil.close(disposableSubscriber);
        disposableSubscriber = new DisposableSubscriber<Long>() {
            @Override
            public void onNext(Long aLong) {
                int time = (int) (60 - aLong);
                tvForgetGetVerificationCode.setText(String.format(App.INSTANCE.getString(R.string.app_count_down_register), time));
            }

            @Override
            public void onError(Throwable t) {
                System.out.println(t.toString());
            }

            @Override
            public void onComplete() {
                tvForgetGetVerificationCode.setText("再次获取验证码");
                tvForgetGetVerificationCode.setEnabled(true);
                if (!disposableSubscriber.isDisposed()) {
                    disposableSubscriber.dispose();
                    disposableSubscriber = null;
                }

            }
        };
        etForgetVerificationCode.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.toString().length() == 6) {
                    tvConfirm.setEnabled(true);
                } else {
                    tvConfirm.setEnabled(false);
                }
            }
        });

        Flowable.interval(0, 1, TimeUnit.SECONDS, Schedulers.newThread())
                .take(60)
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(disposableSubscriber);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        DisposUtil.close(disposableSubscriber);
    }
}
